home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 401-425 / disk_403 / rexxhostlib / fancydemo.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  7KB  |  279 lines

  1. /* $Revision Header * Header built automatically - do not edit! *************
  2.  *
  3.  *    (C) Copyright 1990 by ???
  4.  *
  5.  *    Name .....: FancyDemo.c
  6.  *    Created ..: Monday 07-Mar-88 18:55
  7.  *    Revision .: 5
  8.  *
  9.  *    Date            Author          Comment
  10.  *    =========       ========        ====================
  11.  *    24-May-90       Olsen           Added new functions
  12.  *    19-Mar-90       Olsen           Added new functions
  13.  *    07-Jan-90       Olsen           Integrated rexxhost.library functions
  14.  *    16-Mar-88       Bill Hawes      Added result string return
  15.  *    07-Mar-88       Gary Samad      Created this file!
  16.  *
  17.  ****************************************************************************
  18.  *
  19.  *    FancyDemo.c - A fancy rexx host that can send and receive messages.
  20.  *
  21.  *    This is truly Public Domain!!
  22.  *
  23.  * $Revision Header ********************************************************/
  24.  #define REVISION 5
  25.  
  26. #include <libraries/dosextens.h>
  27. #include "rexxhostbase.h"
  28.  
  29. #ifdef AZTEC_C
  30. #include <functions.h>
  31. #endif    /* AZTEC_C */
  32.  
  33. #define YES    1
  34. #define NO    0
  35.  
  36. #define OK    0
  37. #define NOTOK    1
  38.  
  39. #define EOS    '\0'
  40.  
  41. #define NO_REXX_MSG    "Rexx is not active.  Please run 'rexxmast' from another CLI.\n"
  42. #define STARTUP_MSG    "Type commands to rexx.  Type EOF (^\\) to end.\n"
  43. #define CLOSING_MSG    "Ok, we're closing (after all rexx messages have returned).\n"
  44.  
  45. #define WINDOW_SPEC    "CON:0/10/600/60/Fancy Demo Input Window/c"
  46. #define HOST_PORT_NAME    "FancyDemo"
  47. #define REXX_EXTENSION    "rexx"
  48.  
  49. #define BUFFLEN    100
  50.  
  51.     /* Since we don't need RexxSysBase any more, we take
  52.      * RexxHostBase (interface library).
  53.      */
  54.  
  55. struct RexxHostBase    *RexxHostBase;
  56.  
  57. struct MsgPort        *dos_reply_port;
  58. struct StandardPacket    *dos_message;
  59. struct RexxHost        *rexx_host;
  60. BPTR             window_file_handle;
  61. long             outstanding_rexx_commands = 0;
  62.  
  63.     /******** These are dos functions for getting and displaying user input *******/
  64.  
  65. struct StandardPacket *
  66. setup_dos_message()
  67. {
  68.     struct StandardPacket *malloc();
  69.     struct StandardPacket *new_packet;
  70.  
  71.         /* get a packet */
  72.  
  73.     if(new_packet = malloc(sizeof(struct StandardPacket)))
  74.     {
  75.         /* required AmigaDOS Kludge */
  76.  
  77.         new_packet -> sp_Msg . mn_Node . ln_Name = (char *)&(new_packet -> sp_Pkt);
  78.         new_packet -> sp_Pkt . dp_Link = &(new_packet -> sp_Msg);
  79.     }
  80.  
  81.     return(new_packet);
  82. }
  83.  
  84. void
  85. send_read_packet(dos_message,window_file_handle,dos_reply_port,buff)
  86. struct StandardPacket *dos_message;
  87. BPTR window_file_handle;
  88. struct MsgPort *dos_reply_port;
  89. char *buff;
  90. {
  91.     struct FileHandle *file_handle;
  92.  
  93.         /* change a BPTR to a REAL pointer */
  94.  
  95.     file_handle = (struct FileHandle *)(window_file_handle << 2);
  96.  
  97.         /* setup the packet for reading */
  98.  
  99.     dos_message -> sp_Pkt . dp_Arg1        = file_handle -> fh_Arg1;
  100.     dos_message -> sp_Pkt . dp_Arg2        = (long)buff;
  101.     dos_message -> sp_Pkt . dp_Arg3        = BUFFLEN;
  102.     dos_message -> sp_Pkt . dp_Type        = ACTION_READ;
  103.     dos_message -> sp_Pkt . dp_Port        = dos_reply_port;
  104.     dos_message -> sp_Msg . mn_ReplyPort    = dos_reply_port;
  105.  
  106.         /* now send it */
  107.  
  108.     PutMsg(file_handle -> fh_Type,dos_message);
  109. }
  110.  
  111. void
  112. close_up_shop(value)
  113. long value;
  114. {
  115.     if(window_file_handle)
  116.         Close(window_file_handle);
  117.  
  118.     if(dos_reply_port)
  119.         DeletePort(dos_reply_port);
  120.  
  121.     if(rexx_host)
  122.         rexx_host = DeleteRexxHost(rexx_host);
  123.  
  124.     if(dos_message)
  125.         free(dos_message);
  126.  
  127.     if(RexxHostBase)
  128.         CloseLibrary((struct Library *)RexxHostBase);
  129.  
  130.     exit(value);
  131. }
  132.  
  133. void
  134. main()
  135. {
  136.     long packet_out = NO;        /* whether a READ is outstanding */
  137.     char buff[BUFFLEN+1];        /* used for reading user input */
  138.     struct RexxMsg *rexxmessage;    /* incoming rexx messages */
  139.     long close_down = NO;        /* set when the user hits EOF */
  140.     STRPTR Arg;            /* Temporary string pointer */
  141.     UBYTE ArgBuff[40];        /* Temporary argument buffer */
  142.     LONG ArgCount;            /* Argument counter. */
  143.  
  144.         /* Try to open the rexxhost.library. */
  145.  
  146.     if(!(RexxHostBase = (struct RexxHostBase *)OpenLibrary(REXXHOSTNAME,REXXHOSTMINIMUM)))
  147.     {
  148.         printf("couldn't open rexxhost library.\n");
  149.         close_up_shop(10);
  150.     }
  151.  
  152.         /* open a window to talk to the user through */
  153.  
  154.     if(!(window_file_handle = Open(WINDOW_SPEC,MODE_OLDFILE)))
  155.     {
  156.         printf("sorry, couldn't open a CON: window\n");
  157.         close_up_shop(10);
  158.     }
  159.  
  160.         /* set up a port for dos replies */
  161.  
  162.     if(!(dos_reply_port = (struct MsgPort *)CreatePort(NULL,0)))
  163.     {
  164.         printf("sorry, couldn't set up a dos_reply_port\n");
  165.         close_up_shop(10);
  166.     }
  167.  
  168.         /* set up a public port for rexx to talk to us later */
  169.  
  170.     if(!(rexx_host = CreateRexxHost((STRPTR)HOST_PORT_NAME)))
  171.     {
  172.         printf("sorry, couldn't set up our public rexx port\n");
  173.         close_up_shop(10);
  174.     }
  175.  
  176.         /* set up a dos packet for the asynchronous read from the window */
  177.  
  178.     if(!(dos_message = setup_dos_message()))
  179.     {
  180.         printf("sorry, not enough memory for a dos packet\n");
  181.         close_up_shop(10);
  182.     }
  183.  
  184.     Write(window_file_handle,STARTUP_MSG,sizeof(STARTUP_MSG));
  185.  
  186.         /* loop until quit and no messages outstanding */
  187.  
  188.     while(!close_down || outstanding_rexx_commands)
  189.     {
  190.             /* if the packet (for user input) has not been sent out, send it */
  191.  
  192.         if(!packet_out && !close_down)
  193.         {
  194.             /* send a packet to dos asking for user keyboard input */
  195.  
  196.             send_read_packet(dos_message,window_file_handle,dos_reply_port,buff);
  197.             packet_out = YES;
  198.         }
  199.        
  200.             /* now wait for something to come from the user or from rexx */
  201.  
  202.         Wait((1 << dos_reply_port -> mp_SigBit) | HOSTMASK(rexx_host));
  203.  
  204.             /* got something!! */
  205.  
  206.             /* is it a command from the user? */
  207.  
  208.         if(GetMsg(dos_reply_port))
  209.         {
  210.                 /* not out any more */
  211.  
  212.             packet_out = NO;
  213.  
  214.                 /* if EOF (either the close gadget was hit or ^\) */
  215.  
  216.             if(dos_message -> sp_Pkt . dp_Res1 == 0)
  217.             {
  218.                 close_down = YES;
  219.                 Write(window_file_handle,CLOSING_MSG,sizeof(CLOSING_MSG));
  220.             }
  221.             else
  222.             {
  223.                     /* NULL terminate the string (thanks again DOS!) */
  224.  
  225.                 buff[dos_message -> sp_Pkt . dp_Res1 - 1] = EOS;
  226.  
  227.                     /* send the command directly to rexx */
  228.  
  229.                 if(!SendRexxCommand(rexx_host,(STRPTR)buff,NULL,NULL))
  230.                     Write(window_file_handle,NO_REXX_MSG,sizeof(NO_REXX_MSG));
  231.                 else
  232.                     outstanding_rexx_commands++;
  233.             }
  234.         }
  235.  
  236.             /* did we get something from rexx? */
  237.  
  238.         while(rexxmessage = GetRexxMsg(rexx_host,FALSE))
  239.         {
  240.                 /* Getting a string pointer means
  241.                  * that we've received a command.
  242.                  */
  243.  
  244.             if(Arg = GetRexxCommand(rexxmessage))
  245.             {
  246.                 LONG CharCount = 0; /* Need counter, function reentrant. */
  247.  
  248.                 printf("Got \"%s\" from Rexx.\n",Arg);
  249.  
  250.                     /* Now split the command string into arguments. */
  251.  
  252.                 ArgCount = 0;
  253.  
  254.                 while(GetToken(Arg,&CharCount,ArgBuff,40))
  255.                     printf("Argument %ld = \"%s\"\n",ArgCount++,ArgBuff);
  256.  
  257.                 if(!RexxStrCmp((STRPTR)Arg,(STRPTR)"bad"))
  258.                     ReplyRexxCommand(rexxmessage,10,0,(STRPTR)"A Test");
  259.                 else
  260.                     ReplyRexxCommand(rexxmessage,0,0,(STRPTR)"A Test");
  261.             }
  262.             else
  263.             {
  264.                     /* Now, spill the args... */
  265.  
  266.                 printf("The command \"%s\" has terminated with code %ld, %ld.\n",
  267.                     GetRexxArg(rexxmessage),GetRexxResult1(rexxmessage),GetRexxResult2(rexxmessage));
  268.  
  269.                 FreeRexxCommand(rexxmessage);
  270.                 outstanding_rexx_commands--;
  271.             }
  272.         }
  273.     }
  274.  
  275.         /* clean up */
  276.  
  277.     close_up_shop(0);
  278. }
  279.